package cn.xinfei.xdecision.engine.runner.datax;

import cn.xinfei.xdecision.common.model.datax.exception.XDecisionDataExceptionType;
import cn.xinfei.xdecision.constant.EngineResultEnum;
import cn.xinfei.xdecision.data.RiskDataApiInterface;
import cn.xinfei.xdecision.engine.runner.context.PipelineContextHolder;
import cn.xinfei.xdecision.engine.runner.context.XDecisionContext;
import cn.xinfei.xdecision.engine.runner.metadata.PrometheusTagsConstants;
import cn.xinfei.xdecision.model.VariableApiRequest;
import cn.xinfei.xdecision.model.VariableApiResponse;
import cn.xinfei.xdecision.nacos.NacosSwitchProperties;
import com.alibaba.fastjson.JSONObject;
import com.dianping.cat.Cat;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import cn.xinfei.xdecision.common.utils.util.JsonUtil;

@Data
@Slf4j
@Service
public class DataRealCollectHandler extends RiskDataApiAdapter<VariableApiRequest, Boolean> {

    @Autowired
    private MeterRegistry meterRegistry;

    @Autowired
    private RiskDataApiInterface riskDataApiInterface;


    @Override
    protected Boolean exec(VariableApiRequest variableApiRequest) {

        try {
            VariableApiResponse dataApiResponse = riskDataApiInterface.realCollect(variableApiRequest);
            log.info("DataRealCollectHandler.exec response={}",JsonUtil.toJson(dataApiResponse));

            if (null != dataApiResponse) {
                String code = dataApiResponse.getCode();
                if (String.valueOf(XDecisionDataExceptionType.SUCCESS.code).equals(code)) {
                    String variablesResponse = dataApiResponse.getVariablesResponse();
                    Map fieldInfo = JSONObject.parseObject(variablesResponse, Map.class);

                    if (NacosSwitchProperties.getNacosSwitchProperties().isDebugLog()) {
                        log.info("基于变量中心实时采集的数据，更新上下文。fieldInfo={}", variablesResponse);
                    }
                    PipelineContextHolder.putInContext("real", fieldInfo);
                    return true;
                }
                meterRegistry.counter("xdecision_real_collect_total", PrometheusTagsConstants.XDATA_API_TOTAL_TAGS.and("code", code)).increment();
            } else {
                meterRegistry.counter("xdecision_real_collect_total", PrometheusTagsConstants.XDATA_API_TOTAL_TAGS.and("code", "null")).increment();
            }
        } catch (Exception e) {
            log.error("DataRealCollectHandler exception. error={},variableApiRequest={}", e.getMessage(), JsonUtil.toJson(variableApiRequest), e);
            XDecisionContext.getContext().getStateContext().setEngineResultEnum(EngineResultEnum.DATASOURCE_EXCEPTION);
            Cat.logError(e);
            meterRegistry.counter("xdecision_real_collect_total", PrometheusTagsConstants.XDATA_API_TOTAL_TAGS.and("code", "exception")).increment();
            throw e;
        }

        return false;
    }
}
